package com.laikan.reader.article.adapter.holder;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.laikan.library.managers.ToastMgr;
import com.laikan.reader.R;
import com.laikan.reader.article.activity.BookFocusDetailActivity;
import com.laikan.reader.article.adapter.BookFocusBannerAdapter;
import com.laikan.reader.bookstart.activity.PosterWeb;
import com.laikan.reader.core.bean.BookFocusBean;
import thirdparty.hejunlin.superindicatorlibray.CircleIndicator;
import thirdparty.hejunlin.superindicatorlibray.LoopViewPager;

import java.net.URLDecoder;
import java.util.ArrayList;

/**
 * Created by Administrator on 2016/11/1.
 */
public class BannerHolder extends RecyclerView.ViewHolder {

    public static final String LooperepicAction = "laikan.intent.action.LOOPERPIC";
    private static final int TYPE_ACTIVITY = 1;
    private static final int TYPE_ARTICLE = 2;
    private ArrayList<BookFocusBean.Banner> bannerList;
    private View mRootView;
    private LoopViewPager mLooperViewPager;
    private Context mContext;
    private CircleIndicator indicator;

    public BannerHolder(View view, Context context) {
        super(view);
        mRootView = view;
        mContext = context;
        LooperPicReceiver looperPicReceiver = new LooperPicReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(LooperepicAction);
        mContext.registerReceiver(looperPicReceiver, intentFilter);
        initView(view);
    }

    public View initView(View view) {
        mLooperViewPager = (LoopViewPager)view.findViewById(R.id.viewpager);
        indicator = (CircleIndicator)view.findViewById(R.id.indicator);
        return view;
    }

    public void setData(ArrayList<BookFocusBean.Banner> bannerList) {
        this.bannerList = bannerList;
        //        if (bannerList.size() <= 0) {
        //            getRootView().setVisibility(View.GONE);
        //            return;
        //        }
        refreshView(bannerList);
    }

    public void refreshView(final ArrayList<BookFocusBean.Banner> banner) {
        BookFocusBannerAdapter bookFocusBannerAdapter = new BookFocusBannerAdapter(mContext, banner);
        mLooperViewPager.setAdapter(bookFocusBannerAdapter);
        mLooperViewPager.setOnDispatchTouchEventListener(mDispatchOnTouchListener);
        mLooperViewPager.setLooperPic(true);
        indicator.setViewPager(mLooperViewPager);
        bookFocusBannerAdapter.setOnItemClickListener(new BookFocusBannerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object data) {
                if (banner != null && banner.size() == 0) {
                    ToastMgr.showShortToast(R.string.bookfocus_null_recommend);
                }else{
                    if(Integer.parseInt(banner.get(mLooperViewPager.getCurrentItem()).getType()) == TYPE_ARTICLE){
                        Intent intent = new Intent(mContext, BookFocusDetailActivity.class);
                        intent.putExtra("articleId", banner.get(mLooperViewPager.getCurrentItem()).getUrl());
                        mContext.startActivity(intent);
                    }else if(Integer.parseInt(banner.get(mLooperViewPager.getCurrentItem()).getType()) == TYPE_ACTIVITY){
                        String url = banner.get(mLooperViewPager.getCurrentItem()).getUrl();
                        if (url.contains("title")) {
                            try {
                                String title = url.substring(url.indexOf("=") + 1);
                                title = URLDecoder.decode(title, "utf-8");
                                Intent intent = new Intent();
                                intent.setClass(mContext, PosterWeb.class);
                                intent.putExtra("url", url);
                                intent.putExtra("title", title);
                                mContext.startActivity(intent);
                            }catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
    }

    private LoopViewPager.OnDispatchTouchEventListener mDispatchOnTouchListener = new LoopViewPager.OnDispatchTouchEventListener() {
        @Override
        public void onDispatchKeyEvent(MotionEvent event) {
            Log.d("BannerHolder", "mDispatchOnTouchListener.onDispatchKeyEvent(" + event + ")");
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mLooperViewPager.setLooperPic(false);
            }else if (event.getAction() == MotionEvent.ACTION_UP
                      || event.getAction() == MotionEvent.ACTION_CANCEL) {
                mLooperViewPager.setLooperPic(true);
            }
        }
    };

    public View getRootView() {
        return mRootView;
    }

    private class LooperPicReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && LooperepicAction.equals(intent.getAction()) && mLooperViewPager != null) {
//                boolean isLooperPic = intent.getBooleanExtra("isLooperPic", true);
//                if (isLooperPic) {
//                    mLooperViewPager.setLooperPic(true);
//                }else {
//                    mLooperViewPager.setLooperPic(false);
//                }
            }
        }
    }
}
